package xq.gwt.ui.widgets.entity;

import java.util.ArrayList;

import com.smartgwt.client.widgets.grid.ListGrid;

import xq.gwt.mvc.model.EntityModel;

public class EntityRecordFactory {
	private static ArrayList<EntityRecord> entityRecordPool = new ArrayList<EntityRecord>();
	
	public static EntityRecord getEntityRecordInstance(EntityModel entity, ListGrid grid ){
		EntityRecord entityRecord = null;
		if(!entityRecordPool.isEmpty()){
			entityRecord = entityRecordPool.remove(entityRecordPool.size()-1);
			entityRecord.setEntity(entity);
			entityRecord.setEntityGrid(grid);
		} else {
			entityRecord = new EntityRecord(entity, grid);
		}
		return entityRecord;	
	}
	
	
	public static void recycleEntityRecord(EntityRecord entityRecord){
		if(entityRecordPool.contains(entityRecord))
			return;
		entityRecord.setEntity(null);
		entityRecord.setEntityGrid(null);
		entityRecordPool.add(entityRecord);
	}
	
	public static void recycleEntityRecords(EntityRecord[] entityRecords){
		if(entityRecords == null)
			return;
		for (int i = 0; i < entityRecords.length; i++) {
			recycleEntityRecord(entityRecords[i]);
		}
	}
}
